package leertaak2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Class to output a certain data. This class
 * is modified to the wishes of the customer
 * @author marcovellinga
 *
 */
public class Query {
	
	// Variables
	private File file;
	private BufferedReader in;
	ArrayList<String> stations;
	
	/**
	 * Constructor
	 */
	public Query(){
		file = new File("/Users/marcovellinga/Desktop/WriterProject/allstations.csv");
		stations = new ArrayList<String>();
		getStationNumbers();
		getStationsInfo();
	}
	
	/**
	 * Function to get all the station numbers where
	 * in this case the country is SVALBARD and
	 * put them in an arraylist
	 */
	public void getStationNumbers(){
		
		try {
			
		in = new BufferedReader (new InputStreamReader (new ReverseLineInputStream(file)));
		
		String line = in.readLine();
		    		    
		while (line != null){
			
			int currentPlace = 1;
			String stn = null;
		    String[] dataArray = line.split(",");
			for (String item:dataArray) { 
				if(currentPlace == 1){
					stn = item;
				}
				
				if(currentPlace == 3){
					if(item.equals("SVALBARD")){
						stations.add(stn);
						stn = null;
					}
				}
				currentPlace++;	
			}
			
			line = in.readLine();
		}
		
		in.close();
		} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Get the info of the determined stations
	 * and print them to the console. This also is bound
	 * to some criteria in this case the temperature
	 * between -2 and -5.5
	 */
	public void getStationsInfo(){
		BufferedReader stationFileReader;
		File stationFile;
		try {
		
		for(int i = 0; i < stations.size(); i++){
			System.out.println("###########################");
			System.out.println("STATION: " + stations.get(i));
			System.out.println("---------------------------");
			System.out.println("DATE \t TIME \t TEMP");
			stationFile = new File("/Users/marcovellinga/Desktop/WriterProject/" + stations.get(i) + ".csv");
			
			stationFileReader = new BufferedReader (new InputStreamReader (new FileInputStream(stationFile)));
			
			String line = stationFileReader.readLine();
		    
			while (line != null){
				
				String date = null;
				String time = null;
				String temp = null;
				int currentPlace = 1;
			    String[] dataArray = line.split(",");
				for (String item:dataArray) { 
					if(currentPlace == 1){
						date = item;
					}
					if(currentPlace == 2){
						time = item;
					}
					if(currentPlace == 3){
						if(Double.parseDouble(item) < -2.0 && Double.parseDouble(item) > -5.5){
							temp = item;
							System.out.print(date + "\t");
							System.out.print(time + "\t");
							System.out.print(temp + "\t");
							System.out.println();
							break;
						}
						break;
					}
					
					currentPlace++;	
				}
				
				
				line = stationFileReader.readLine();
			}
			
			stationFileReader.close();
			
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
}
